 <template>
  <Dialog :title="dialogTitle" v-model="dialogStudentVisible">
    <ContentWrap>
      <el-form
        class="-mb-15px"
        :model="queryParams"
        ref="queryFormRef"
        :inline="true"
        label-width="68px"
      >
        <el-form-item label="用户名称" prop="username">
          <el-input
            v-model="queryParams.username"
            placeholder="请输入用户名称"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item label="手机号码" prop="mobile">
          <el-input
            v-model="queryParams.mobile"
            placeholder="请输入手机号码"
            clearable
            @keyup.enter="handleQuery"
            class="!w-240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button @click="handleQuery"><Icon icon="ep:search" />搜索</el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>
    
    <ContentWrap>
      <el-table v-loading="loading" :data="tableData" :stripe="true" @selection-change="handleSelectionChange">
        <el-table-column label="序号" type="index"  header-align="center" align="center" width="60px" fixed />
        <el-table-column type="selection" width="55" />
        <el-table-column label="用户昵称" align="center" prop="nickname" :show-overflow-tooltip="true" />
        <el-table-column label="部门" align="center" key="deptName" prop="deptName" :show-overflow-tooltip="true" />
        <el-table-column label="手机号码" align="center" prop="mobile" width="120" />
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
    <template #footer>
      <el-button @click="submitForm" type="primary">确 定</el-button>
      <el-button @click="dialogStudentVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import * as UserApi from '@/api/system/user'

import { dateFormatter } from '@/utils/formatTime'
import { getIntDictOptions } from '@/utils/dict'
import { DICT_TYPE } from '@/utils/dict'
import { ref, defineEmits } from 'vue'

/** 课程管理 列表 */
defineOptions({ name: 'StudentSelectForm' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const dialogStudentVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const queryFormRef = ref() // 搜索的表单

const loading = ref(true) // 列表的加载中
const tableData = ref<UserApi.UserVO[]>([]) // 列表的数据
const selectData = ref<UserApi.UserVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  username: undefined,
  mobile: undefined,
  status: 0,
  deptId: undefined,
  createTime: []
})

/** 打开弹窗 */
const open =  async () => {
  dialogStudentVisible.value = true;
  loading.value = false;
  dialogTitle.value = "选择公函人员"
  getList()
}
defineExpose({ open }); // 提供 open 方法，用于打开弹窗

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields()
  handleQuery()
}

const handleSelectionChange = (val: UserApi.UserVO[]) => {
  selectData.value = []
  for (let i = 0; i < val.length; i++) {
    selectData.value.push(val[i])
  }
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await UserApi.getUserPage(queryParams)
    tableData.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 提交表单 */
const emit = defineEmits(['sendData']) // 定义 success 事件，用于操作成功后的回调
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const submitForm = async () => {
  // 提交请求
  formLoading.value = true
  try {
    dialogStudentVisible.value = false
	  emit('sendData', selectData)
  } finally {
    formLoading.value = false
  }
}
</script>
<style scoped>
.ttitle {
  padding: 20px 40px;
  background: #fff;
  border: 1px solid var(--el-border-color-light);
  margin-bottom: 15px;
}

.ttop {
  font-size: 24px;
  line-height: 40px;
}

.tcontent {
  font-size: 16px;
  line-height: 30px;
  color: #363535ab;
}

.dis {
  display: flex;
  height: 20px;
  line-height: 20px;
}

:deep(.el-form-item__label) {
  width: auto !important;
}

:deep(.el-tabs__nav-scroll) {
  background: #fff;
  padding: 0 30px;
  /*font-size:16px */
}

:deep(.el-tabs__header) {
  margin: 0;
}

:deep(.el-tabs) {
  background: #fff;
}

.el-card {
  margin-top: 15px;
}

:deep(.el-table .cell) {
  text-overflow: initial;
}

:deep(.el-tabs__content) {
  background: #f8fbff;
}

:deep(.-mb-15px) {
  margin-top: 10px;
}
</style>
